<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.ico?v=7.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.ico?v=7.0.0">


  <link rel="mask-icon" href="/images/logo.svg?v=7.0.0" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '7.0.0',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="注：本文转载自南轲梦 上篇文章《深入浅出Mybatis系列（四）—配置详解之typeAliases别名（mybatis源码篇）》为大家介绍了mybatis中别名的使用，以及其源码。本篇将为大家介绍TypeHandler， 并简单分析其源码。 Mybatis中的TypeHandler是什么？ 无论是 MyBatis 在预处理语句（PreparedStatement）中设置一个参数时，还是从结果集中取">
<meta name="keywords" content="Mybatis,Source code">
<meta property="og:type" content="article">
<meta property="og:title" content="深入浅出Mybatis系列五-TypeHandler简介及配置（mybatis源码篇）">
<meta property="og:url" content="https://chloneda.github.io/blog/mybatis-5/index.html">
<meta property="og:site_name" content="Chloneda">
<meta property="og:description" content="注：本文转载自南轲梦 上篇文章《深入浅出Mybatis系列（四）—配置详解之typeAliases别名（mybatis源码篇）》为大家介绍了mybatis中别名的使用，以及其源码。本篇将为大家介绍TypeHandler， 并简单分析其源码。 Mybatis中的TypeHandler是什么？ 无论是 MyBatis 在预处理语句（PreparedStatement）中设置一个参数时，还是从结果集中取">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-05-19T14:53:29.163Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="深入浅出Mybatis系列五-TypeHandler简介及配置（mybatis源码篇）">
<meta name="twitter:description" content="注：本文转载自南轲梦 上篇文章《深入浅出Mybatis系列（四）—配置详解之typeAliases别名（mybatis源码篇）》为大家介绍了mybatis中别名的使用，以及其源码。本篇将为大家介绍TypeHandler， 并简单分析其源码。 Mybatis中的TypeHandler是什么？ 无论是 MyBatis 在预处理语句（PreparedStatement）中设置一个参数时，还是从结果集中取">



  <link rel="alternate" href="/rss2.html" title="Chloneda" type="application/atom+xml">




  <link rel="canonical" href="https://chloneda.github.io/blog/mybatis-5/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>深入浅出Mybatis系列五-TypeHandler简介及配置（mybatis源码篇） | Chloneda</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chloneda</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">简约的程序员</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-project">

    
    
    
      
    

    

    <a href="/project/" rel="section"><i class="menu-item-icon fa fa-fw fa-github"></i> <br>项目</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-links">

    
    
    
      
    

    

    <a href="/links/" rel="section"><i class="menu-item-icon fa fa-fw fa-link"></i> <br>友链</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  
    

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://chloneda.github.io/blog/mybatis-5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="chloneda">
      <meta itemprop="description" content="Less is more">
      <meta itemprop="image" content="/images/chloneda.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chloneda">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">深入浅出Mybatis系列五-TypeHandler简介及配置（mybatis源码篇）

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-05-19 22:53:29" itemprop="dateCreated datePublished" datetime="2019-05-19T22:53:29+08:00">2019-05-19</time>
            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Mybatis/" itemprop="url" rel="index"><span itemprop="name">Mybatis</span></a></span>

                
                
              
            </span>
          

          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon">
            <i class="fa fa-eye"></i>
             阅读次数： 
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>注：本文转载自<a href="https://www.cnblogs.com/dongying/p/4040435.html" target="_blank" rel="noopener">南轲梦</a></p>
<p>上篇文章《深入浅出Mybatis系列（四）—配置详解之typeAliases别名（mybatis源码篇）》为大家介绍了mybatis中别名的使用，以及其源码。本篇将为大家介绍TypeHandler， 并简单分析其源码。</p>
<p>Mybatis中的TypeHandler是什么？</p>
<p>无论是 MyBatis 在预处理语句（PreparedStatement）中设置一个参数时，还是从结果集中取出一个值时，都会用类型处理器将获取的值以合适的方式转换成 Java 类型。Mybatis默认为我们实现了许多TypeHandler, 当我们没有配置指定TypeHandler时，Mybatis会根据参数或者返回结果的不同，默认为我们选择合适的TypeHandler处理。</p>
<p>那么，Mybatis为我们实现了哪些TypeHandler呢?  我们怎么自定义实现一个TypeHandler ?  这些都会在接下来的mybatis的源码中看到。</p>
<p>在看源码之前，还是像之前一样，先看看怎么配置吧？</p>
<h1 id="配置TypeHandler"><a href="#配置TypeHandler" class="headerlink" title="配置TypeHandler"></a>配置TypeHandler</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">typeHandlers</span>&gt;</span></span><br><span class="line">      <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">          当配置package的时候，mybatis会去配置的package扫描TypeHandler</span></span><br><span class="line"><span class="comment">          &lt;package name="com.dy.demo"/&gt;</span></span><br><span class="line"><span class="comment">       --&gt;</span></span><br><span class="line">      </span><br><span class="line">      <span class="comment">&lt;!-- handler属性直接配置我们要指定的TypeHandler --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">typeHandler</span> <span class="attr">handler</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">      </span><br><span class="line">      <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">          javaType 配置java类型，例如String, 如果配上javaType, </span></span><br><span class="line"><span class="comment">	　那么指定的typeHandler就只作用于指定的类型 </span></span><br><span class="line"><span class="comment">　　　　--&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">typeHandler</span> <span class="attr">javaType</span>=<span class="string">""</span> <span class="attr">handler</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">      </span><br><span class="line">      <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">          jdbcType 配置数据库基本数据类型，例如varchar, 如果配上jdbcType,</span></span><br><span class="line"><span class="comment">　　　　　  那么指定的typeHandler就只作用于指定的类型  </span></span><br><span class="line"><span class="comment">　　　　--&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">typeHandler</span> <span class="attr">jdbcType</span>=<span class="string">""</span> <span class="attr">handler</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">      </span><br><span class="line">      <span class="comment">&lt;!-- 也可两者都配置 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">typeHandler</span> <span class="attr">javaType</span>=<span class="string">""</span> <span class="attr">jdbcType</span>=<span class="string">""</span> <span class="attr">handler</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">      </span><br><span class="line">  <span class="tag">&lt;/<span class="name">typeHandlers</span>&gt;</span></span><br><span class="line">  </span><br><span class="line">  ......</span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>上面简单介绍了一下TypeHandler,  下面就看看mybatis中TypeHandler的源码了。</p>
<p>老规矩，先从对xml的解析讲起：</p>
<h1 id="typeHandlers节点源码"><a href="#typeHandlers节点源码" class="headerlink" title="typeHandlers节点源码"></a>typeHandlers节点源码</h1><figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 解析typeHandlers节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> typeHandlerElement(XNode parent) throws Exception &#123;</span><br><span class="line">    <span class="built_in">if</span> (parent != null) &#123;</span><br><span class="line">      <span class="built_in">for</span> (XNode child : parent.getChildren()) &#123;</span><br><span class="line">        <span class="comment">//子节点为package时，获取其name属性的值，然后自动扫描package下的自定义typeHandler</span></span><br><span class="line">        <span class="built_in">if</span> (<span class="string">"package"</span>.equals(child.getName())) &#123;</span><br><span class="line">          <span class="keyword">String</span> typeHandlerPackage = child.getStringAttribute(<span class="string">"name"</span>);</span><br><span class="line">          typeHandlerRegistry.<span class="keyword">register</span>(typeHandlerPackage);</span><br><span class="line">        &#125; <span class="built_in">else</span> &#123;</span><br><span class="line">          <span class="comment">/**</span></span><br><span class="line"><span class="comment">          * 子节点为typeHandler时， 可以指定javaType属性， </span></span><br><span class="line"><span class="comment">          * 也可以指定jdbcType, 也可两者都指定</span></span><br><span class="line"><span class="comment">          * javaType 是指定java类型</span></span><br><span class="line"><span class="comment">          * jdbcType 是指定jdbc类型（数据库类型： 如varchar）</span></span><br><span class="line"><span class="comment">          */</span></span><br><span class="line">          <span class="keyword">String</span> javaTypeName = child.getStringAttribute(<span class="string">"javaType"</span>);</span><br><span class="line">          <span class="keyword">String</span> jdbcTypeName = child.getStringAttribute(<span class="string">"jdbcType"</span>);</span><br><span class="line">          <span class="comment">//handler就是我们配置的typeHandler</span></span><br><span class="line">          <span class="keyword">String</span> handlerTypeName = child.getStringAttribute(<span class="string">"handler"</span>);</span><br><span class="line"></span><br><span class="line">          <span class="comment">//resolveClass方法就是我们上篇文章所讲的TypeAliasRegistry里面处理别名的方法</span></span><br><span class="line">          Class&lt;?&gt; javaTypeClass = resolveClass(javaTypeName);</span><br><span class="line">          <span class="comment">//JdbcType是一个枚举类型，resolveJdbcType方法是在获取枚举类型的值</span></span><br><span class="line">          JdbcType jdbcType = resolveJdbcType(jdbcTypeName);</span><br><span class="line">          Class&lt;?&gt; typeHandlerClass = resolveClass(handlerTypeName);</span><br><span class="line"></span><br><span class="line">          <span class="comment">//注册typeHandler, typeHandler通过TypeHandlerRegistry这个类管理</span></span><br><span class="line">          <span class="built_in">if</span> (javaTypeClass != null) &#123;</span><br><span class="line">            <span class="built_in">if</span> (jdbcType == null) &#123;</span><br><span class="line">              typeHandlerRegistry.<span class="keyword">register</span>(javaTypeClass, typeHandlerClass);</span><br><span class="line">            &#125; <span class="built_in">else</span> &#123;</span><br><span class="line">              typeHandlerRegistry.<span class="keyword">register</span>(javaTypeClass, jdbcType, typeHandlerClass);</span><br><span class="line">            &#125;</span><br><span class="line">          &#125; <span class="built_in">else</span> &#123;</span><br><span class="line">            typeHandlerRegistry.<span class="keyword">register</span>(typeHandlerClass);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>接下来看看TypeHandler的管理注册类：</p>
<h1 id="TypeHandlerRegistry源码"><a href="#TypeHandlerRegistry源码" class="headerlink" title="TypeHandlerRegistry源码"></a>TypeHandlerRegistry源码</h1><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * typeHandler注册管理类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">class</span> TypeHandlerRegistry &#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//源码一上来，二话不说，几个大大的HashMap就出现，这不又跟上次讲的typeAliases的注册类似么</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">//基本数据类型与其包装类</span></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Map&lt;<span class="keyword">Class</span>&lt;?&gt;, <span class="keyword">Class</span>&lt;?&gt;&gt; reversePrimitiveMap </span><br><span class="line">	= <span class="keyword">new</span> HashMap&lt;<span class="keyword">Class</span>&lt;?&gt;, <span class="keyword">Class</span>&lt;?&gt;&gt;() &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1</span>L;</span><br><span class="line">    &#123;</span><br><span class="line">      put(<span class="keyword">Byte</span>.<span class="keyword">class</span>, <span class="keyword">byte</span>.<span class="keyword">class</span>);</span><br><span class="line">      put(<span class="keyword">Short</span>.<span class="keyword">class</span>, <span class="keyword">short</span>.<span class="keyword">class</span>);</span><br><span class="line">      put(Integer.<span class="keyword">class</span>, <span class="keyword">int</span>.<span class="keyword">class</span>);</span><br><span class="line">      put(<span class="keyword">Long</span>.<span class="keyword">class</span>, <span class="keyword">long</span>.<span class="keyword">class</span>);</span><br><span class="line">      put(<span class="keyword">Float</span>.<span class="keyword">class</span>, <span class="keyword">float</span>.<span class="keyword">class</span>);</span><br><span class="line">      put(<span class="keyword">Double</span>.<span class="keyword">class</span>, <span class="keyword">double</span>.<span class="keyword">class</span>);</span><br><span class="line">      put(<span class="keyword">Boolean</span>.<span class="keyword">class</span>, <span class="keyword">boolean</span>.<span class="keyword">class</span>);</span><br><span class="line">      put(Character.<span class="keyword">class</span>, <span class="keyword">char</span>.<span class="keyword">class</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//这几个MAP不用说就知道存的是什么东西吧，命名的好处</span></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">final</span> Map&lt;JdbcType, TypeHandler&lt;?&gt;&gt; JDBC_TYPE_HANDLER_MAP </span><br><span class="line">		= <span class="keyword">new</span> EnumMap&lt;JdbcType, TypeHandler&lt;?&gt;&gt;(JdbcType.<span class="keyword">class</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">final</span> Map&lt;Type, Map&lt;JdbcType, TypeHandler&lt;?&gt;&gt;&gt; TYPE_HANDLER_MAP </span><br><span class="line">		= <span class="keyword">new</span> HashMap&lt;Type, Map&lt;JdbcType, TypeHandler&lt;?&gt;&gt;&gt;();</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">final</span> TypeHandler&lt;Object&gt; UNKNOWN_TYPE_HANDLER </span><br><span class="line">		= <span class="keyword">new</span> UnknownTypeHandler(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">final</span> Map&lt;<span class="keyword">Class</span>&lt;?&gt;, TypeHandler&lt;?&gt;&gt; ALL_TYPE_HANDLERS_MAP </span><br><span class="line">		= <span class="keyword">new</span> HashMap&lt;<span class="keyword">Class</span>&lt;?&gt;, TypeHandler&lt;?&gt;&gt;();</span><br><span class="line"></span><br><span class="line">  <span class="comment">//就像上篇文章讲的typeAliases一样，mybatis也默认给我们注册了不少的typeHandler</span></span><br><span class="line">  <span class="comment">//具体如下</span></span><br><span class="line">  <span class="keyword">public</span> TypeHandlerRegistry() &#123;</span><br><span class="line">    register(<span class="keyword">Boolean</span>.<span class="keyword">class</span>, <span class="keyword">new</span> BooleanTypeHandler());</span><br><span class="line">    register(<span class="keyword">boolean</span>.<span class="keyword">class</span>, <span class="keyword">new</span> BooleanTypeHandler());</span><br><span class="line">    register(JdbcType.<span class="keyword">BOOLEAN</span>, <span class="keyword">new</span> BooleanTypeHandler());</span><br><span class="line">    register(JdbcType.BIT, <span class="keyword">new</span> BooleanTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(<span class="keyword">Byte</span>.<span class="keyword">class</span>, <span class="keyword">new</span> ByteTypeHandler());</span><br><span class="line">    register(<span class="keyword">byte</span>.<span class="keyword">class</span>, <span class="keyword">new</span> ByteTypeHandler());</span><br><span class="line">    register(JdbcType.TINYINT, <span class="keyword">new</span> ByteTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(<span class="keyword">Short</span>.<span class="keyword">class</span>, <span class="keyword">new</span> ShortTypeHandler());</span><br><span class="line">    register(<span class="keyword">short</span>.<span class="keyword">class</span>, <span class="keyword">new</span> ShortTypeHandler());</span><br><span class="line">    register(JdbcType.SMALLINT, <span class="keyword">new</span> ShortTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(Integer.<span class="keyword">class</span>, <span class="keyword">new</span> IntegerTypeHandler());</span><br><span class="line">    register(<span class="keyword">int</span>.<span class="keyword">class</span>, <span class="keyword">new</span> IntegerTypeHandler());</span><br><span class="line">    register(JdbcType.INTEGER, <span class="keyword">new</span> IntegerTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(<span class="keyword">Long</span>.<span class="keyword">class</span>, <span class="keyword">new</span> LongTypeHandler());</span><br><span class="line">    register(<span class="keyword">long</span>.<span class="keyword">class</span>, <span class="keyword">new</span> LongTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(<span class="keyword">Float</span>.<span class="keyword">class</span>, <span class="keyword">new</span> FloatTypeHandler());</span><br><span class="line">    register(<span class="keyword">float</span>.<span class="keyword">class</span>, <span class="keyword">new</span> FloatTypeHandler());</span><br><span class="line">    register(JdbcType.<span class="keyword">FLOAT</span>, <span class="keyword">new</span> FloatTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(<span class="keyword">Double</span>.<span class="keyword">class</span>, <span class="keyword">new</span> DoubleTypeHandler());</span><br><span class="line">    register(<span class="keyword">double</span>.<span class="keyword">class</span>, <span class="keyword">new</span> DoubleTypeHandler());</span><br><span class="line">    register(JdbcType.<span class="keyword">DOUBLE</span>, <span class="keyword">new</span> DoubleTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(String.<span class="keyword">class</span>, <span class="keyword">new</span> StringTypeHandler());</span><br><span class="line">    register(String.<span class="keyword">class</span>, JdbcType.<span class="keyword">CHAR</span>, <span class="keyword">new</span> StringTypeHandler());</span><br><span class="line">    register(String.<span class="keyword">class</span>, JdbcType.CLOB, <span class="keyword">new</span> ClobTypeHandler());</span><br><span class="line">    register(String.<span class="keyword">class</span>, JdbcType.VARCHAR, <span class="keyword">new</span> StringTypeHandler());</span><br><span class="line">    register(String.<span class="keyword">class</span>, JdbcType.LONGVARCHAR, <span class="keyword">new</span> ClobTypeHandler());</span><br><span class="line">    register(String.<span class="keyword">class</span>, JdbcType.NVARCHAR, <span class="keyword">new</span> NStringTypeHandler());</span><br><span class="line">    register(String.<span class="keyword">class</span>, JdbcType.NCHAR, <span class="keyword">new</span> NStringTypeHandler());</span><br><span class="line">    register(String.<span class="keyword">class</span>, JdbcType.NCLOB, <span class="keyword">new</span> NClobTypeHandler());</span><br><span class="line">    register(JdbcType.<span class="keyword">CHAR</span>, <span class="keyword">new</span> StringTypeHandler());</span><br><span class="line">    register(JdbcType.VARCHAR, <span class="keyword">new</span> StringTypeHandler());</span><br><span class="line">    register(JdbcType.CLOB, <span class="keyword">new</span> ClobTypeHandler());</span><br><span class="line">    register(JdbcType.LONGVARCHAR, <span class="keyword">new</span> ClobTypeHandler());</span><br><span class="line">    register(JdbcType.NVARCHAR, <span class="keyword">new</span> NStringTypeHandler());</span><br><span class="line">    register(JdbcType.NCHAR, <span class="keyword">new</span> NStringTypeHandler());</span><br><span class="line">    register(JdbcType.NCLOB, <span class="keyword">new</span> NClobTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(Object.<span class="keyword">class</span>, JdbcType.ARRAY, <span class="keyword">new</span> ArrayTypeHandler());</span><br><span class="line">    register(JdbcType.ARRAY, <span class="keyword">new</span> ArrayTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(BigInteger.<span class="keyword">class</span>, <span class="keyword">new</span> BigIntegerTypeHandler());</span><br><span class="line">    register(JdbcType.BIGINT, <span class="keyword">new</span> LongTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(BigDecimal.<span class="keyword">class</span>, <span class="keyword">new</span> BigDecimalTypeHandler());</span><br><span class="line">    register(JdbcType.REAL, <span class="keyword">new</span> BigDecimalTypeHandler());</span><br><span class="line">    register(JdbcType.DECIMAL, <span class="keyword">new</span> BigDecimalTypeHandler());</span><br><span class="line">    register(JdbcType.NUMERIC, <span class="keyword">new</span> BigDecimalTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(<span class="keyword">Byte</span>[].<span class="keyword">class</span>, <span class="keyword">new</span> ByteObjectArrayTypeHandler());</span><br><span class="line">    register(<span class="keyword">Byte</span>[].<span class="keyword">class</span>, JdbcType.BLOB, <span class="keyword">new</span> BlobByteObjectArrayTypeHandler());</span><br><span class="line">    register(<span class="keyword">Byte</span>[].<span class="keyword">class</span>, JdbcType.LONGVARBINARY,<span class="keyword">new</span> BlobByteObjectArrayTypeHandler());</span><br><span class="line">    register(<span class="keyword">byte</span>[].<span class="keyword">class</span>, <span class="keyword">new</span> ByteArrayTypeHandler());</span><br><span class="line">    register(<span class="keyword">byte</span>[].<span class="keyword">class</span>, JdbcType.BLOB, <span class="keyword">new</span> BlobTypeHandler());</span><br><span class="line">    register(<span class="keyword">byte</span>[].<span class="keyword">class</span>, JdbcType.LONGVARBINARY, <span class="keyword">new</span> BlobTypeHandler());</span><br><span class="line">    register(JdbcType.LONGVARBINARY, <span class="keyword">new</span> BlobTypeHandler());</span><br><span class="line">    register(JdbcType.BLOB, <span class="keyword">new</span> BlobTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(Object.<span class="keyword">class</span>, UNKNOWN_TYPE_HANDLER);</span><br><span class="line">    register(Object.<span class="keyword">class</span>, JdbcType.OTHER, UNKNOWN_TYPE_HANDLER);</span><br><span class="line">    register(JdbcType.OTHER, UNKNOWN_TYPE_HANDLER);</span><br><span class="line"></span><br><span class="line">    register(Date.<span class="keyword">class</span>, <span class="keyword">new</span> DateTypeHandler());</span><br><span class="line">    register(Date.<span class="keyword">class</span>, JdbcType.DATE, <span class="keyword">new</span> DateOnlyTypeHandler());</span><br><span class="line">    register(Date.<span class="keyword">class</span>, JdbcType.TIME, <span class="keyword">new</span> TimeOnlyTypeHandler());</span><br><span class="line">    register(JdbcType.TIMESTAMP, <span class="keyword">new</span> DateTypeHandler());</span><br><span class="line">    register(JdbcType.DATE, <span class="keyword">new</span> DateOnlyTypeHandler());</span><br><span class="line">    register(JdbcType.TIME, <span class="keyword">new</span> TimeOnlyTypeHandler());</span><br><span class="line"></span><br><span class="line">    register(java.sql.Date.<span class="keyword">class</span>, <span class="keyword">new</span> SqlDateTypeHandler());</span><br><span class="line">    register(java.sql.Time.<span class="keyword">class</span>, <span class="keyword">new</span> SqlTimeTypeHandler());</span><br><span class="line">    register(java.sql.Timestamp.<span class="keyword">class</span>, <span class="keyword">new</span> SqlTimestampTypeHandler());</span><br><span class="line"></span><br><span class="line">    <span class="comment">// issue #273</span></span><br><span class="line">    register(Character.<span class="keyword">class</span>, <span class="keyword">new</span> CharacterTypeHandler());</span><br><span class="line">    register(<span class="keyword">char</span>.<span class="keyword">class</span>, <span class="keyword">new</span> CharacterTypeHandler());</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">boolean</span> hasTypeHandler(<span class="keyword">Class</span>&lt;?&gt; javaType) &#123;</span><br><span class="line">    <span class="keyword">return</span> hasTypeHandler(javaType, <span class="keyword">null</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">boolean</span> hasTypeHandler(TypeReference&lt;?&gt; javaTypeReference) &#123;</span><br><span class="line">    <span class="keyword">return</span> hasTypeHandler(javaTypeReference, <span class="keyword">null</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">boolean</span> hasTypeHandler(<span class="keyword">Class</span>&lt;?&gt; javaType, JdbcType jdbcType) &#123;</span><br><span class="line">    <span class="keyword">return</span> javaType != <span class="keyword">null</span> &amp;&amp; getTypeHandler((Type) javaType, jdbcType) != <span class="keyword">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">boolean</span> hasTypeHandler(TypeReference&lt;?&gt; javaTypeReference,</span><br><span class="line">		JdbcType jdbcType) &#123;</span><br><span class="line">    <span class="keyword">return</span> javaTypeReference != <span class="keyword">null</span> </span><br><span class="line">		&amp;&amp; getTypeHandler(javaTypeReference, jdbcType) != <span class="keyword">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> TypeHandler&lt;?&gt; getMappingTypeHandler(</span><br><span class="line">		<span class="keyword">Class</span>&lt;? <span class="keyword">extends</span> TypeHandler&lt;?&gt;&gt; handlerType) &#123;</span><br><span class="line">    <span class="keyword">return</span> ALL_TYPE_HANDLERS_MAP.get(handlerType);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; TypeHandler&lt;T&gt; getTypeHandler(<span class="keyword">Class</span>&lt;T&gt; type) &#123;</span><br><span class="line">    <span class="keyword">return</span> getTypeHandler((Type) type, <span class="keyword">null</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; TypeHandler&lt;T&gt; getTypeHandler(TypeReference&lt;T&gt; javaTypeReference) &#123;</span><br><span class="line">    <span class="keyword">return</span> getTypeHandler(javaTypeReference, <span class="keyword">null</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> TypeHandler&lt;?&gt; getTypeHandler(JdbcType jdbcType) &#123;</span><br><span class="line">    <span class="keyword">return</span> JDBC_TYPE_HANDLER_MAP.get(jdbcType);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; TypeHandler&lt;T&gt; getTypeHandler(<span class="keyword">Class</span>&lt;T&gt; type, JdbcType jdbcType) &#123;</span><br><span class="line">    <span class="keyword">return</span> getTypeHandler((Type) type, jdbcType);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; TypeHandler&lt;T&gt; getTypeHandler(</span><br><span class="line">		TypeReference&lt;T&gt; javaTypeReference, JdbcType jdbcType) &#123;</span><br><span class="line">    <span class="keyword">return</span> getTypeHandler(javaTypeReference.getRawType(), jdbcType);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> &lt;T&gt; TypeHandler&lt;T&gt; getTypeHandler(Type type, JdbcType jdbcType) &#123;</span><br><span class="line">    Map&lt;JdbcType, TypeHandler&lt;?&gt;&gt; jdbcHandlerMap = TYPE_HANDLER_MAP.get(type);</span><br><span class="line">    TypeHandler&lt;?&gt; handler = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (jdbcHandlerMap != <span class="keyword">null</span>) &#123;</span><br><span class="line">      handler = jdbcHandlerMap.get(jdbcType);</span><br><span class="line">      <span class="keyword">if</span> (handler == <span class="keyword">null</span>) &#123;</span><br><span class="line">        handler = jdbcHandlerMap.get(<span class="keyword">null</span>);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (handler == <span class="keyword">null</span> </span><br><span class="line">	&amp;&amp; type != <span class="keyword">null</span> </span><br><span class="line">	&amp;&amp; type <span class="keyword">instanceof</span> <span class="keyword">Class</span> </span><br><span class="line">	&amp;&amp; Enum.<span class="keyword">class</span>.isAssignableFrom((<span class="keyword">Class</span>&lt;?&gt;) type)) &#123;</span><br><span class="line">      handler = <span class="keyword">new</span> EnumTypeHandler((<span class="keyword">Class</span>&lt;?&gt;) type);</span><br><span class="line">    &#125;</span><br><span class="line">    @SuppressWarnings(<span class="string">"unchecked"</span>)</span><br><span class="line">    <span class="comment">// type drives generics here</span></span><br><span class="line">    TypeHandler&lt;T&gt; returned = (TypeHandler&lt;T&gt;) handler;</span><br><span class="line">    <span class="keyword">return</span> returned;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> TypeHandler&lt;Object&gt; getUnknownTypeHandler() &#123;</span><br><span class="line">    <span class="keyword">return</span> UNKNOWN_TYPE_HANDLER;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> register(JdbcType jdbcType, TypeHandler&lt;?&gt; handler) &#123;</span><br><span class="line">    JDBC_TYPE_HANDLER_MAP.put(jdbcType, handler);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//</span></span><br><span class="line">  <span class="comment">// REGISTER INSTANCE</span></span><br><span class="line">  <span class="comment">//</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 只配置了typeHandler, 没有配置jdbcType 或者javaType</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  @SuppressWarnings(<span class="string">"unchecked"</span>)</span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> register(TypeHandler&lt;T&gt; typeHandler) &#123;</span><br><span class="line">    <span class="keyword">boolean</span> mappedTypeFound = <span class="keyword">false</span>;</span><br><span class="line">    <span class="comment">//在自定义typeHandler的时候，可以加上注解MappedTypes 去指定关联的javaType</span></span><br><span class="line">    <span class="comment">//因此，此处需要扫描MappedTypes注解</span></span><br><span class="line">    MappedTypes mappedTypes = typeHandler.getClass().getAnnotation(MappedTypes.<span class="keyword">class</span>);</span><br><span class="line">    <span class="keyword">if</span> (mappedTypes != <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">Class</span>&lt;?&gt; handledType : mappedTypes.value()) &#123;</span><br><span class="line">        register(handledType, typeHandler);</span><br><span class="line">        mappedTypeFound = <span class="keyword">true</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// @since 3.1.0 - try to auto-discover the mapped type</span></span><br><span class="line">    <span class="keyword">if</span> (!mappedTypeFound &amp;&amp; typeHandler <span class="keyword">instanceof</span> TypeReference) &#123;</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">        TypeReference&lt;T&gt; typeReference = (TypeReference&lt;T&gt;) typeHandler;</span><br><span class="line">        register(typeReference.getRawType(), typeHandler);</span><br><span class="line">        mappedTypeFound = <span class="keyword">true</span>;</span><br><span class="line">      &#125; <span class="keyword">catch</span> (Throwable t) &#123;</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">        * maybe users define the TypeReference with a different </span></span><br><span class="line"><span class="comment">        * type and are not assignable, so just ignore it</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (!mappedTypeFound) &#123;</span><br><span class="line">      register((<span class="keyword">Class</span>&lt;T&gt;) <span class="keyword">null</span>, typeHandler);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 配置了typeHandlerhe和javaType</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> register(<span class="keyword">Class</span>&lt;T&gt; javaType, TypeHandler&lt;? <span class="keyword">extends</span> T&gt; typeHandler) &#123;</span><br><span class="line">    register((Type) javaType, typeHandler);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> &lt;T&gt; <span class="keyword">void</span> register(Type javaType, TypeHandler&lt;? <span class="keyword">extends</span> T&gt; typeHandler) &#123;</span><br><span class="line">    <span class="comment">//扫描注解MappedJdbcTypes</span></span><br><span class="line">    MappedJdbcTypes mappedJdbcTypes </span><br><span class="line">			= typeHandler.getClass().getAnnotation(MappedJdbcTypes.<span class="keyword">class</span>);</span><br><span class="line">    <span class="keyword">if</span> (mappedJdbcTypes != <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">for</span> (JdbcType handledJdbcType : mappedJdbcTypes.value()) &#123;</span><br><span class="line">        register(javaType, handledJdbcType, typeHandler);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (mappedJdbcTypes.includeNullJdbcType()) &#123;</span><br><span class="line">        register(javaType, <span class="keyword">null</span>, typeHandler);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      register(javaType, <span class="keyword">null</span>, typeHandler);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> register(TypeReference&lt;T&gt; javaTypeReference, </span><br><span class="line">		TypeHandler&lt;? <span class="keyword">extends</span> T&gt; handler) &#123;</span><br><span class="line">    register(javaTypeReference.getRawType(), handler);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * typeHandlerhe、javaType、jdbcType都配置了</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> register(<span class="keyword">Class</span>&lt;T&gt; type, </span><br><span class="line">				JdbcType jdbcType, </span><br><span class="line">				TypeHandler&lt;? <span class="keyword">extends</span> T&gt; handler) &#123;</span><br><span class="line">    register((Type) type, jdbcType, handler);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 注册typeHandler的核心方法</span></span><br><span class="line"><span class="comment">   * 就是向Map新增数据而已</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">void</span> register(Type javaType, JdbcType jdbcType, TypeHandler&lt;?&gt; handler) &#123;</span><br><span class="line">    <span class="keyword">if</span> (javaType != <span class="keyword">null</span>) &#123;</span><br><span class="line">      Map&lt;JdbcType, TypeHandler&lt;?&gt;&gt; map = TYPE_HANDLER_MAP.get(javaType);</span><br><span class="line">      <span class="keyword">if</span> (map == <span class="keyword">null</span>) &#123;</span><br><span class="line">        map = <span class="keyword">new</span> HashMap&lt;JdbcType, TypeHandler&lt;?&gt;&gt;();</span><br><span class="line">        TYPE_HANDLER_MAP.put(javaType, map);</span><br><span class="line">      &#125;</span><br><span class="line">      map.put(jdbcType, handler);</span><br><span class="line">      <span class="keyword">if</span> (reversePrimitiveMap.containsKey(javaType)) &#123;</span><br><span class="line">        register(reversePrimitiveMap.get(javaType), jdbcType, handler);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ALL_TYPE_HANDLERS_MAP.put(handler.getClass(), handler);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//</span></span><br><span class="line">  <span class="comment">// REGISTER CLASS</span></span><br><span class="line">  <span class="comment">//</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// Only handler type</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> register(<span class="keyword">Class</span>&lt;?&gt; typeHandlerClass) &#123;</span><br><span class="line">    <span class="keyword">boolean</span> mappedTypeFound = <span class="keyword">false</span>;</span><br><span class="line">    MappedTypes mappedTypes = typeHandlerClass.getAnnotation(MappedTypes.<span class="keyword">class</span>);</span><br><span class="line">    <span class="keyword">if</span> (mappedTypes != <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">Class</span>&lt;?&gt; javaTypeClass : mappedTypes.value()) &#123;</span><br><span class="line">        register(javaTypeClass, typeHandlerClass);</span><br><span class="line">        mappedTypeFound = <span class="keyword">true</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (!mappedTypeFound) &#123;</span><br><span class="line">      register(getInstance(<span class="keyword">null</span>, typeHandlerClass));</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// java type + handler type</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> register(<span class="keyword">Class</span>&lt;?&gt; javaTypeClass, <span class="keyword">Class</span>&lt;?&gt; typeHandlerClass) &#123;</span><br><span class="line">    register(javaTypeClass, getInstance(javaTypeClass, typeHandlerClass));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// java type + jdbc type + handler type</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> register(<span class="keyword">Class</span>&lt;?&gt; javaTypeClass, </span><br><span class="line">			JdbcType jdbcType, </span><br><span class="line">			<span class="keyword">Class</span>&lt;?&gt; typeHandlerClass) &#123;</span><br><span class="line">    register(javaTypeClass, jdbcType, getInstance(javaTypeClass, typeHandlerClass));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Construct a handler (used also from Builders)</span></span><br><span class="line"></span><br><span class="line">  @SuppressWarnings(<span class="string">"unchecked"</span>)</span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; TypeHandler&lt;T&gt; getInstance(<span class="keyword">Class</span>&lt;?&gt; javaTypeClass, </span><br><span class="line">					<span class="keyword">Class</span>&lt;?&gt; typeHandlerClass) &#123;</span><br><span class="line">    <span class="keyword">if</span> (javaTypeClass != <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">        Constructor&lt;?&gt; c = typeHandlerClass.getConstructor(<span class="keyword">Class</span>.<span class="keyword">class</span>);</span><br><span class="line">        <span class="keyword">return</span> (TypeHandler&lt;T&gt;) c.newInstance(javaTypeClass);</span><br><span class="line">      &#125; <span class="keyword">catch</span> (NoSuchMethodException ignored) &#123;</span><br><span class="line">        <span class="comment">// ignored</span></span><br><span class="line">      &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> TypeException(<span class="string">"Failed invoking constructor for handler "</span> </span><br><span class="line">		+ typeHandlerClass, e);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      Constructor&lt;?&gt; c = typeHandlerClass.getConstructor();</span><br><span class="line">      <span class="keyword">return</span> (TypeHandler&lt;T&gt;) c.newInstance();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> TypeException(<span class="string">"Unable to find a usable constructor for "</span> </span><br><span class="line">			+ typeHandlerClass, e);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 根据指定的pacakge去扫描自定义的typeHander，然后注册</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> register(String packageName) &#123;</span><br><span class="line">    ResolverUtil&lt;<span class="keyword">Class</span>&lt;?&gt;&gt; resolverUtil = <span class="keyword">new</span> ResolverUtil&lt;<span class="keyword">Class</span>&lt;?&gt;&gt;();</span><br><span class="line">    resolverUtil.<span class="keyword">find</span>(<span class="keyword">new</span> ResolverUtil.IsA(TypeHandler.<span class="keyword">class</span>), packageName);</span><br><span class="line">    Set&lt;<span class="keyword">Class</span>&lt;? <span class="keyword">extends</span> <span class="keyword">Class</span>&lt;?&gt;&gt;&gt; handlerSet = resolverUtil.getClasses();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">Class</span>&lt;?&gt; type : handlerSet) &#123;</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">      * Ignore inner classes and interfaces (including package-info.java) </span></span><br><span class="line"><span class="comment">      * and abstract classes</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">      <span class="keyword">if</span> (!type.isAnonymousClass() </span><br><span class="line">		&amp;&amp; !type.isInterface() </span><br><span class="line">		&amp;&amp; !Modifier.isAbstract(type.getModifiers())) &#123;</span><br><span class="line">        register(type);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// get information</span></span><br><span class="line">  </span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 通过configuration对象可以获取已注册的所有typeHandler</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">public</span> Collection&lt;TypeHandler&lt;?&gt;&gt; getTypeHandlers() &#123;</span><br><span class="line">    <span class="keyword">return</span> Collections.unmodifiableCollection(ALL_TYPE_HANDLERS_MAP.values());</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由源码可以看到， mybatis为我们实现了那么多TypeHandler,  随便打开一个TypeHandler，看其源码，都可以看到，它继承自一个抽象类：BaseTypeHandler， 那么我们是不是也能通过继承BaseTypeHandler，从而实现自定义的TypeHandler ? 答案是肯定的， 那么现在下面就为大家演示一下自定义TypeHandler:</p>
<h1 id="自定义TypeHandler"><a href="#自定义TypeHandler" class="headerlink" title="自定义TypeHandler"></a>自定义TypeHandler</h1><figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@MappedJdbcTypes</span>(JdbcType.VARCHAR)  </span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 此处如果不用注解指定jdbcType, 那么，就可以在配置文件中通过"jdbcType"属性指定，</span></span><br><span class="line"><span class="comment">* 同理， javaType 也可通过 <span class="doctag">@MappedTypes</span>指定</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ExampleTypeHandler</span> <span class="keyword">extends</span> <span class="title">BaseTypeHandler</span>&lt;<span class="title">String</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> setNonNullParameter(PreparedStatement ps</span><br><span class="line">		, <span class="keyword">int</span> i</span><br><span class="line">		, String parameter</span><br><span class="line">		, JdbcType jdbcType) <span class="keyword">throws</span> SQLException &#123;</span><br><span class="line">    ps.setString(i, parameter);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> String getNullableResult(ResultSet rs</span><br><span class="line">		, String columnName) <span class="keyword">throws</span> SQLException &#123;</span><br><span class="line">    <span class="function"><span class="keyword">return</span> rs.<span class="title">getString</span><span class="params">(columnName)</span></span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> <span class="function">String <span class="title">getNullableResult</span><span class="params">(ResultSet rs, <span class="keyword">int</span> columnIndex)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">return</span> rs.<span class="title">getString</span><span class="params">(columnIndex)</span></span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> String getNullableResult(CallableStatement cs, <span class="keyword">int</span> columnIndex) </span><br><span class="line">			<span class="keyword">throws</span> SQLException &#123;</span><br><span class="line">    <span class="function"><span class="keyword">return</span> cs.<span class="title">getString</span><span class="params">(columnIndex)</span></span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后，就该配置我们的自定义TypeHandler了：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">typeHandlers</span>&gt;</span></span><br><span class="line">      <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">	由于自定义的TypeHandler在定义时已经通过注解指定了jdbcType, 所以此处不用再配置jdbcType</span></span><br><span class="line"><span class="comment">      --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">typeHandler</span> <span class="attr">handler</span>=<span class="string">"ExampleTypeHandler"</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">typeHandlers</span>&gt;</span></span><br><span class="line">  </span><br><span class="line">  ......</span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>也就是说，我们在自定义TypeHandler的时候，可以在TypeHandler通过@MappedJdbcTypes指定jdbcType, 通过 @MappedTypes 指定javaType, 如果没有使用注解指定，那么我们就需要在配置文件中配置。</p>
<p>好啦，本篇文章到此结束。</p>
<hr>

      
    </div>

    

    
    
    

    

    
      
    
    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>您的支持是对我最大的鼓励</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById(&quot;QR&quot;); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="chloneda 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="chloneda 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        



  



<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>chloneda</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    
    <a href="https://chloneda.github.io/blog/mybatis-5/" title="深入浅出Mybatis系列五-TypeHandler简介及配置（mybatis源码篇）">https://chloneda.github.io/blog/mybatis-5/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Mybatis/" rel="tag"># Mybatis</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/mybatis-9/" rel="next" title="深入浅出Mybatis系列九-强大的动态SQL">
                <i class="fa fa-chevron-left"></i> 深入浅出Mybatis系列九-强大的动态SQL
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/snmp-protocol/" rel="prev" title="聊聊SNMP协议">
                聊聊SNMP协议 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>
  
  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/chloneda.jpeg" alt="chloneda">
            
              <p class="site-author-name" itemprop="name">chloneda</p>
              <p class="site-description motion-element" itemprop="description">Less is more</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">31</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">16</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">16</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/rss2.html" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/chloneda" title="GitHub &rarr; https://github.com/chloneda" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://twitter.com/chloneda" title="Twitter &rarr; https://twitter.com/chloneda" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:chloneda@gmail.com" title="E-Mail &rarr; mailto:chloneda@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://weibo.com/chloneda" title="Weibo &rarr; https://weibo.com/chloneda" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="http://www.zhihu.com/people/chl_vip" title="知乎 &rarr; http://www.zhihu.com/people/chl_vip" rel="noopener" target="_blank"><i class="fa fa-fw fa-globe"></i>知乎</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://www.cnblogs.com/chloneda" title="博客园 &rarr; https://www.cnblogs.com/chloneda" rel="noopener" target="_blank"><i class="fa fa-fw fa-globe"></i>博客园</a>
                </span>
              
            </div>
          

          

          
          


	  <div id="music163player">
		<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=5239700&auto=0&height=66"></iframe>
	  </div>

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#配置TypeHandler"><span class="nav-number">1.</span> <span class="nav-text">配置TypeHandler</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#typeHandlers节点源码"><span class="nav-number">2.</span> <span class="nav-text">typeHandlers节点源码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#TypeHandlerRegistry源码"><span class="nav-number">3.</span> <span class="nav-text">TypeHandlerRegistry源码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#自定义TypeHandler"><span class="nav-number">4.</span> <span class="nav-text">自定义TypeHandler</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chloneda</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> v7.0.0</div>




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="总访客量">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="post-meta-divider">|</span>
  

  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="总访问量">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.0"></script>

  <script src="/js/src/motion.js?v=7.0.0"></script>



  
  


  <script src="/js/src/schemes/muse.js?v=7.0.0"></script>




  
  <script src="/js/src/scrollspy.js?v=7.0.0"></script>
<script src="/js/src/post-details.js?v=7.0.0"></script>



  


  <script src="/js/src/bootstrap.js?v=7.0.0"></script>



  



  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://www.gstatic.com/firebasejs/4.6.0/firebase.js"></script>
  <script src="https://www.gstatic.com/firebasejs/4.6.0/firebase-firestore.js"></script>
  
  <script>
    (function () {

      firebase.initializeApp({
        apiKey: '',
        projectId: ''
      })

      function getCount(doc, increaseCount) {
        //increaseCount will be false when not in article page

        return doc.get().then(function (d) {
          var count
          if (!d.exists) { //has no data, initialize count
            if (increaseCount) {
              doc.set({
                count: 1
              })
              count = 1
            }
            else {
              count = 0
            }
          }
          else { //has data
            count = d.data().count
            if (increaseCount) {
              if (!(window.localStorage && window.localStorage.getItem(title))) { //if first view this article
                doc.set({ //increase count
                  count: count + 1
                })
                count++
              }
            }
          }
          if (window.localStorage && increaseCount) { //mark as visited
            localStorage.setItem(title, true)
          }

          return count
        })
      }

      function appendCountTo(el) {
        return function (count) {
          $(el).append(
            $('<span>').addClass('post-visitors-count').append(
              $('<span>').addClass('post-meta-divider').text('|')
            ).append(
              $('<span>').addClass('post-meta-item-icon').append(
                $('<i>').addClass('fa fa-users')
              )
              ).append($('<span>').text('阅读次数 ' + count))
          )
        }
      }

      var db = firebase.firestore()
      var articles = db.collection('articles')

      //https://hexo.io/docs/variables.html
      var isPost = '深入浅出Mybatis系列五-TypeHandler简介及配置（mybatis源码篇）'.length > 0
      var isArchive = '' === 'true'
      var isCategory = ''.length > 0
      var isTag = ''.length > 0

      if (isPost) { //is article page
        var title = '深入浅出Mybatis系列五-TypeHandler简介及配置（mybatis源码篇）'
        var doc = articles.doc(title)

        getCount(doc, true).then(appendCountTo($('.post-meta')))
      }
      else if (!isArchive && !isCategory && !isTag) { //is index page
        var titles = [] //array to titles

        var postsstr = '' //if you have a better way to get titles of posts, please change it
        eval(postsstr)

        var promises = titles.map(function (title) {
          return articles.doc(title)
        }).map(function (doc) {
          return getCount(doc)
        })
        Promise.all(promises).then(function (counts) {
          var metas = $('.post-meta')
          counts.forEach(function (val, idx) {
            appendCountTo(metas[idx])(val)
          })
        })
      }
    })()
  </script>


  

  

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>


  

  

  

  

  

  

  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function(i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap');
      $(e).after($wrap);
      $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
        var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
          return $(e).text();
        }).toArray().join('\n');
        var ta = document.createElement('textarea');
        var range = document.createRange(); //For Chrome
        var sel = window.getSelection(); //For Chrome
        var yPosition = window.pageYOffset || document.documentElement.scrollTop;
        ta.style.top = yPosition + 'px'; //Prevent page scroll
        ta.style.position = 'absolute';
        ta.style.opacity = '0';
        ta.value = code;
        ta.textContent = code; //For FireFox
        ta.contentEditable = true;
        ta.readOnly = false;
        document.body.appendChild(ta);
        range.selectNode(ta);
        sel.removeAllRanges();
        sel.addRange(range);
        ta.setSelectionRange(0, code.length);
        var result = document.execCommand('copy');
        
        ta.blur(); //For iOS
        $(this).blur();
      })).on('mouseleave', function(e) {
        var $b = $(this).find('.copy-btn');
        setTimeout(function() {
          $b.text('复制');
        }, 300);
      }).append(e);
    })
  </script>


</body>
</html>
